var createError = require('http-errors');
var express = require('express');
//引入body-parse插件
const bodyParser = require('body-parser');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var chkRouter = require('./routes/chktoken');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var loginRouter = require('./routes/login');
var menuRouter = require('./routes/menu');
var rightsRouter = require('./routes/rights');
var goodsRouter = require('./routes/goods');
var ordersRouter = require('./routes/order');


var app = express();
app.all('*', function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    // 设置允许添加的请求头
    res.header('Access-Control-Allow-Headers', 'Content-Type, authorization');
    res.header('Access-Control-Allow-Methods', '*');
    res.header('Content-Type', 'application/json;charset=utf-8');
    next();
});
/***z
 * 改写原配置在www的信息
 * 1.复制www文件中的如下代码到app.js
 *  var http = require('http');
 *  var server = http.createServer(app);
 *
 * 2.删除app.js文件中的如下代码，不将app对象暴露出去
 *  module.exports = app;
 *
 *  3.增加监听端口号
 *  server.listen('8080')
 *
 *  4.修改配置启动项
 *  --原项目是执行bin/www文件
 *  --修改为执行根目录下的app.js文件启动
 *  --原bin目录即可删除
 */

var http = require('http');
var server = http.createServer(app);

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
//注册body-parse插件-处理post请求
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static(path.join(__dirname, 'public')));

// 解析token获取用户信息
app.use(chkRouter)

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/login', loginRouter);
app.use('/menu', menuRouter);
app.use('/rights', rightsRouter);
app.use('/goods',goodsRouter);
app.use('/orders',ordersRouter);
server.listen('9090');
